package com.lmt.fallback;

import com.netflix.hystrix.exception.HystrixTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * Provider回退类
 * Date 2018/06/30
 *
 * @author MT-Lin
 */
@SuppressWarnings("ALL")
@Component
public class ProviderFallbackProvider implements FallbackProvider {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 表明该回退类为哪个微服务提供回退
     * @return *和null表示为所有的微服务提供默认回退类
     */
    @Override
    public String getRoute() {
        return "provider";
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        logger.warn("gateway-zuul-fallback->The micro service(Provider) has some problem {}"
        , cause.getCause().getMessage());
        if (cause instanceof HystrixTimeoutException) {
            return response(HttpStatus.GATEWAY_TIMEOUT);
        } else {
            return response(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    private ClientHttpResponse response(final HttpStatus status){
        return new ClientHttpResponse() {

            /**
             * fallback时的状态码
             * @return 状态码
             * @throws IOException IO异常
             */
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return status;
            }

            /**
             * 数字类型的状态码
             * @return status.value
             * @throws IOException IO异常
             */
            @Override
            public int getRawStatusCode() throws IOException {
                return status.value();
            }

            /**
             * 状态文本
             * @return text
             * @throws IOException IO异常
             */
            @Override
            public String getStatusText() throws IOException {
                return status.getReasonPhrase();
            }

            @Override
            public void close() {
            }

            /**
             * 响应体
             * @return 微服务不可用
             * @throws IOException IO异常
             */
            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream("The micro service(Provider) is not available, please try again later!".getBytes());
            }

            /**
             * headers设定
             * @return HttpHeaders
             */
            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                MediaType mediaType = new MediaType("application", "json", Charset.forName("UTF-8"));
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}
